<!--
* @Author: zhangjing
* @Date: 2024-07-06 18:06:59
* @Description: 代付确认框=>
-->
<template>
	<u-popup :show="showModal" @close="close" mode="center" :round="20">
		<view class="cu-dialog">
			<view class="hd">
				<image src="https://hyb.huaxianpai.com/upload/static/hyb/behalf/behalf.png" mode="widthFix"></image>
				<view>确认找人帮忙支付吗？</view>
			</view>
			<view class="content flex flex-direction">
				<view class="title flex-sub">
					<view class="u-m-t-35">请在10分钟内完成支付</view>
					<view class="u-m-t-25">好友支付成功后请及时跟客服联系，避免出现漏单情况</view>
				</view>
				<!-- #ifdef MP-WEIXIN -->
				<button class="btn" open-type="share">转发分享</button>
				<!-- #endif -->
				<!-- #ifdef APP-PLUS -->
				<button class="btn" @tap="getQrcode('share')">转发分享</button>
				<!-- #endif -->
				<button class="btn" @tap="getQrcode">下载保存二维码分享</button>
			</view>
			<image class="close" src="https://hyb.huaxianpai.com/upload/static/hyb/behalf/close.png" mode="widthFix" @click="hideModal"></image>
		</view>
	</u-popup>
</template>

<script>
// #ifdef APP-PLUS
import { mapState } from 'vuex';
import { appShare, closeAppShare } from '@/utils/appshare/appshare';
// #endif
export default {
	name: 'behalfMod',
	data() {
		return {
			showModal: false,
			userId: '',
			orderNo: '',
			qrCode: '',
			type: 2
		};
	},
	// #ifdef APP-PLUS
	computed: {
		...mapState(['userInfo'])
	},
	// #endif
	methods: {
		show(userId, orderNo, type) {
			this.orderNo = orderNo;
			this.userId = userId;
			this.type = type || 2;
			this.showModal = true;
		},
		hideModal() {
			this.showModal = false;
			this.$emit('hideModal');
		},
		/* 获取小程序骂 */
		getQrcode(type) {
			this.$postAction(this.$api.getQrcode, {
				scene: this.userId + '#' + this.orderNo + '#' + this.type,
				page: 'apppay/behalf/behalf'
			}).then(da => {
				if (da.code == 0) {
					this.qrCode = da.data.img_url;
					this.showModal = false;
					// #ifdef APP-PLUS
					if (type == 'share') {
						this.onAppShare();
					} else {
						this.downloadFile();
					}
					// #endif
					// #ifdef MP-WEIXIN
					this.onDown();
					// #endif
					this.$emit('hideModal');
				}
			});
		},
		onDown() {
			let that = this;
			let index = 0;
			// console.log('触发下载图片');
			// 第一步，先调用授权功能
			uni.authorize({
				scope: 'scope.writePhotosAlbum',
				success: () => {
					this.downloadFile();
				},
				fail() {
					// 2、授权失败 弹框再次要求授权
					uni.showModal({
						title: '您需要授权相册权限',
						success(res) {
							// 2.1点击确认按钮就调取授权设置页面
							if (res.confirm) {
								// 2.2 开启授权设置页面
								uni.openSetting({
									success(res) {},
									fail(res) {}
								});
							}
						}
					});
				}
			});
		},
		downloadFile() {
			// 1.1 调用下载api方法
			uni.downloadFile({
				url: this.qrCode, // 获取要下载的服务器里的图片地址
				success: res => {
					// 1.2 获取远程图片地址后,将图片地址缓存到本地
					if (res.statusCode === 200) {
						// console.log(res, '============下载后的图片地址=============>');
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath, // 把远程的图片地址及图片保存到本地
							success: function (res) {
								// 1.3保存成功后弹框提示保存成功
								uni.showToast({
									title: '保存成功',
									icon: 'none'
								});
							},
							fail: function (res) {
								// console.log(res, '============fail=============>');
								// 1.4保存失败给用户弹框提示
								uni.showToast({
									title: '保存失败',
									icon: 'none'
								});
								if (res.errMsg == 'saveImageToPhotosAlbum:fail cancel') {
									return;
								}
							}
						});
					}
				},
				fail(e) {
					console.log('下载失败', e);
				}
			});
		},
		// #ifdef APP-PLUS
		// APP分享
		onAppShare() {
			/* 只分享到微信聊天界面 */
			uni.share({
				provider: 'weixin',
				scene: 'WXSceneSession',
				type: 2,
				imageUrl: this.qrCode,
				success: function (res) {
					console.log('success:' + JSON.stringify(res));
				},
				fail: function (err) {
					console.log('fail:' + JSON.stringify(err));
				}
			});
			return;
			let shareUrl = 'https://m.huaxianpai.com/wap/share/index.html';
			let shareAppUrl = '/apppay/behalf/behalf';
			shareAppUrl += '?userId=' + this.userInfo.id + '&orderNo=' + this.orderNo;
			let shareName = '来自' + this.userInfo.wx_name + '的代付';
			// #ifdef APP-PLUS
			let shareObj = appShare(
				{
					shareUrl: shareUrl,
					shareTitle: '花比三家',
					shareContent: shareName,
					shareImg: 'https://m.huaxianpai.com/upload/images/999213530.jpg',
					appId: 'wx5ca2585337f0f40e', // 默认不传type的时候，必须传appId和appPath才会显示小程序图标
					appPath: shareAppUrl,
					appWebUrl: shareUrl
				},
				res => {
					console.log('分享');
					closeAppShare();
				}
			);
			// setTimeout(() => {
			// 	shareObj.close();
			// }, 5000);
			// #endif
			// #ifndef APP-PLUS
			uni.showToast({
				title: '请在APP环境使用',
				icon: 'none'
			});
			// #endif
		}
		// #endif
	}
};
</script>

<style lang="less" scoped>
.cu-dialog {
	background: transparent;
	overflow: visible;
	padding-bottom: 36rpx;
	width: 580rpx;
	.hd {
		position: relative;
		image {
			display: block;
			width: 100%;
		}
		view {
			position: absolute;
			font-weight: 500;
			font-size: 36rpx;
			color: #ffffff;
			line-height: 50rpx;
			top: 65rpx;
			left: 28rpx;
		}
	}
	.content {
		font-size: 32rpx;
		color: #1c1c1e;
		line-height: 45rpx;
		padding: 0 28rpx;
		.title > view {
			position: relative;
			padding-left: 24rpx;
			&::before {
				content: '';
				display: block;
				width: 8rpx;
				height: 8rpx;
				background: var(--nav-bg);
				border-radius: 50upx;
				position: absolute;
				left: 0;
				top: 20rpx;
			}
		}

		.btn {
			width: 524rpx;
			height: 88upx;
			display: block;
			border-radius: 50upx;
			box-shadow: 0px 12rpx 24rpx -9rpx #ffbec2;
			background: linear-gradient(90deg, #ff8173 0%, #ff3543 100%);
			color: #fff;
			font-size: 28upx;
			font-weight: bold;
			line-height: 88upx;
			margin-top: 40upx;
			& + .btn {
				color: var(--nav-bg);
				border: 1px solid var(--nav-bg);
				box-shadow: none;
				margin-top: 36upx;
				background: none;
			}
		}
	}
}

.close {
	width: 100upx;
	height: 100upx;
	position: absolute;
	bottom: -144upx;
	left: 50%;
	transform: translateX(-50%);
}
</style>
